package com.htfyun.eink.pw.core.ctrl;

import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Build;
import android.view.Display;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.htfyun.eink.pw.core.IImeStateListener;
import com.htfyun.eink.pw.core.IOnDrawListener;
import com.htfyun.eink.pw.core.IPointerEventListener;
import com.htfyun.eink.pw.core.enums.PWA2Gate;
import com.htfyun.eink.pw.core.enums.PWAction;
import com.htfyun.eink.pw.core.enums.PWDataMode;
import com.htfyun.eink.pw.core.enums.PWDisplayMode;

/**
 * 操作底层的手写
 */
public final class PWCoreHelper {

    /**
     * 初始化 PW 底层. 支持反复设置, 因为底层做了判断的
     */
    public static void initForPW() {
        PWCoreController.getInstance().initForPw();
    }

    /**
     * 反初始化 PW 底层. 支持反复设置, 因为底层做了判断的
     */
    public static void deinitForPW() {
        PWCoreController.getInstance().deinitForPw();
    }

    public static void setPWViewOffsetXY(Bitmap bmp, int offsetX, int offsetY) {
        PWCoreController.getInstance().setPWViewOffsetXY(bmp, offsetX, offsetY);
    }

    public static void setHostBmpOffsetXY(Bitmap bmp, int offsetX, int offsetY) {
        PWCoreController.getInstance().setHostBmpOffsetXY(bmp, offsetX, offsetY);
    }

    public static void updatePwCanvasRotation(@Nullable Display display) {
        if (display == null) {
            return;
        }
        PWCoreController.getInstance().updatePwCanvasRotation(display);
    }

    public static void postRectForPw(Rect rectDirty) {
        postRectForPw(rectDirty,
                PWDisplayMode.EINKMODE_GLAUTO,
                PWA2Gate.EinkA2GateForA16);
    }

    public static void postRectForPw(Rect rectDirty, PWDisplayMode displayMode, PWA2Gate pwA2Gate) {
        PWCoreController.getInstance().updateRectForPw(rectDirty,
                displayMode,
                PWDataMode.EINK_DATA_FASTPW,
                pwA2Gate);
    }

    public static void postRectForHost(Rect rectDirty) {
        PWCoreController.getInstance().updateRectForHost(rectDirty,
                PWDisplayMode.EINKMODE_GLAUTO,
                PWDataMode.EinkDataModeA16,
                PWA2Gate.EinkA2GateForA16);
    }

    public static void postRectForHost(Rect rectDirty, PWDisplayMode displayMode, PWDataMode dataMode, PWA2Gate pwA2Gate) {
        PWCoreController.getInstance().updateRectForHost(rectDirty, displayMode, dataMode, pwA2Gate);
    }

    public static void setPWAction(@NonNull PWAction action) {
        if (Build.VERSION.SDK_INT >= 30) {
            PWCoreController.getInstance().setPWAction(action.getValue());
        }
    }

    //---设置监听
    public static void setPointerEventListenerForView(@Nullable View view, @Nullable IPointerEventListener listener) {
        if (view == null) {
            return;
        }
        PWCoreController.getInstance().setPointerEventListenerForView(view, listener);
    }

    public static void setOnDrawListenerForView(@Nullable View view, @Nullable IOnDrawListener listener) {
        if (view == null) {
            return;
        }
        PWCoreController.getInstance().setOnDrawListenerForView(view, listener);
    }

    public static void registerImeStateListener(@NonNull IImeStateListener listener) {
        PWCoreController.getInstance().registerImeStateListener(listener);
    }

    public static void unregisterImeStateListener(@NonNull IImeStateListener listener) {
        PWCoreController.getInstance().unregisterImeStateListener(listener);
    }

    //////////////////////////////
    //以下与 pw 手写无关.
    public static void setEinkUpdateModeWithView(@NonNull View view, int dataMode, int dispMode) {
        PWCoreController.getInstance().setEinkUpdateModeWithView(view, dataMode, dispMode);
    }

    public static void setValueAnimatorDurationScale(float durationScale) {
        PWCoreController.getInstance().setValueAnimatorDurationScale(durationScale);
    }

    @NonNull
    public static String getSystemProperties(String key) {
        return PWCoreController.getInstance().getSystemProperties(key);
    }

    public static void setSystemProperties(String key, String val) {
        PWCoreController.getInstance().setSystemProperties(key, val);
    }

    public static void setSystemPropertiesBool(String key, boolean val) {
        PWCoreController.getInstance().setSystemProperties(key, val ? "1" : "0");
    }

    public static boolean getSystemPropertiesBool(String key, boolean def) {
        String value = getSystemProperties(key);
        int len = value.length();
        boolean result = def;
        if (len == 1) {
            if (value.equals("0") || value.equals("n"))
                result = false;
            else if (value.equals("1") || value.equals("y"))
                result = true;
        } else if (len > 1) {
            if (value.equals("no") || value.equals("false") || value.equals("off")) {
                result = false;
            } else if (value.equals("yes") || value.equals("true") || value.equals("on")) {
                result = true;
            }
        }

        return result;
    }
    //以上与 pw 手写无关.
    //////////////////////////////


}
